define([
    'text!./qrcode_widget.html',
    '../../service',
    '../../utils/i18n',
    'qrcode'
], function(sourceHTML, api, i18n) {

    var widget = avalon.ui.qrcodewidget = function(element, data, vmodels) {
        
        var vmodel = avalon.define(data.qrcodewidgetId, function(vm) {

            vm.visible = false;
            vm.$setting = {};
            _.merge(vm,data.qrcodewidgetOptions)

            vm.showShareCode = function() {
            
                vm.visible = true;
                $("#qrcode").html("")
                $("#qrcode").qrcode({ width: 185, height: 185,text: location.href});
             
            }
            vm.hideShareCode = function() {
                vm.visible = false
            }
            
            window._bd_share_config = {
                "common": {
                    "bdSnsKey": {},
                    "bdText": (vm.Video&&vm.Video.title)||"",
                    "bdMini": "2",
                    "bdMiniList": false,
                    "bdPic": "",
                    "bdStyle": "0",
                    "bdSize": "16",
                    "bdUrl": location.href
                }, 
                "share": {},
               
                "selectShare": {
                    "bdContainerClass": null,
                    "bdSelectMiniList": ["qzone", "tsina", "tqq", "renren", "weixin"]
                }
            };

            vm.$init = function() {
                vm.$setting = avalon.vmodels.app.$setting;
                window._bd_share_main = null
                window._bd_share_is_recently_loaded = false;
                avalon.getScript('http://bdimg.share.baidu.com/static/api/js/share.js?cdnversion=' + ~(-new Date() / 36e5))
                element.innerHTML = sourceHTML;
                avalon.scan(element, [vmodel].concat(vmodels));
            }
            vm.$remove = function() {
                element.innerHTML = element.textContent = "";
            }

        })

        return vmodel;
    }
    return avalon

})
